<template>
  <!-- :color="value ? '#ffa500' : ''" -->
  <van-icon
    :name="value ? 'star' : 'star-o'"
    :class="{
      collected: value
    }"
    :loading="Loading"
    @click="onCollect"
  />
</template>

<script>
import { addCollect, deleteCollect } from '../../api/user'
export default {
  name: 'CollectArticle',
  props: {
    value: {
      type: Boolean,
      required: true
    },
    articleId: {
      type: [String, Object, Number],
      required: true
    }
  },
  data() {
    return {
      Loading: false
    }
  },
  methods: {
    async onCollect() {
      this.Loading = true
      try {
        if (this.value) {
          // 已收藏
          await deleteCollect(this.articleId)
        } else {
          await addCollect(this.articleId)
        }
        this.$emit('input', !this.value)
        this.$nextTick(() => {
          this.$toast.success(this.value ? '收藏成功' : '取消收藏')
        })
      } catch (err) {
        this.$toast('操作失败')
      }
      this.Loading = false
    }
  }
}
</script>

<style lang="less" scoped>
.collected {
  color: #ffa500 !important;
}
</style>
